//处理文件名，示例，将./h5_李大任.html改为./h5_lidare.html，initPath为要处理的文件夹路径，这个还会查找文件内的"李大任"改为"lidare"，内部引用文件的路径会自动跟着改变
const fs = require("fs");
const oldNameRec = /李大任/g;
const newName = "lidare";
const initPath = "./";
const reNameFun = (components = [], path = initPath) => {
  // console.log("path: " + path);
  const files = fs.readdirSync(path);
  files.forEach(item => {
    // console.log(item);
    if(/sloveName.js/g.test(item))return;
    components.push(item);
    let stat = fs.statSync(path + item);
    if (stat.isDirectory() === true) {
      return reNameFun([], path + item + "/");
    } else {
      // console.log("item: " + item);
      fs.readFile(item, 'utf8', (err, data) => {
        // console.log(data);
        if (!data) return;
        data = data.replace(oldNameRec, newName);
        fs.writeFile(item, data, () => {});
      });
    }
  });
  // console.log(components);
  components.forEach(item => {
    const filePath = path + item;
    const newPath = filePath.replace(oldNameRec, newName);
    fs.rename(filePath, newPath, () => {});
  });
  return 0;
}
reNameFun();